package com.catersystem.zmy.catersystem.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.bean.TypeBean;

import java.util.List;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class TypeAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<TypeBean> mData;

    public TypeAdapter(Context context, List<TypeBean> data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }


    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.elv_type_item, null);
            holder.tvType = (TextView) convertView.findViewById(R.id.tv_type);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvType.setText(mData.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition).getChildType().size();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (groupPosition >= 0) {
            return mData.get(groupPosition).getChildType().size();
        } else {
            return 0;
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup
            parent) {
        ChildViewHolder holder = null;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = View.inflate(mContext, R.layout.elv_child_type_item, null);
            holder.childTvType = (TextView) convertView.findViewById(R.id.tv_child_type);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.childTvType.setText(mData.get(groupPosition).getChildType().get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class ViewHolder {
        private TextView tvType;
    }

    class ChildViewHolder {
        private TextView childTvType;
    }
}
